import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class HZLoadingDialog {
  Duration displayDuration;
  /// success widget of loading
  Widget successWidget;
  /// error widget of loading
  Widget errorWidget;

  factory HZLoadingDialog() => _instance;
  static final HZLoadingDialog _instance = HZLoadingDialog._internal();

  HZLoadingDialog._internal();
  static TransitionBuilder init({
    TransitionBuilder builder,
  }) {
    EasyLoading.instance
      ..displayDuration = const Duration(milliseconds: 1500)
      ..indicatorType = EasyLoadingIndicatorType.fadingCircle
      ..loadingStyle = EasyLoadingStyle.custom
      ..indicatorSize = 45.0
      ..radius = 8.0
      ..progressColor = Colors.white
      ..backgroundColor = Color.fromARGB(180, 0, 0, 0)
      ..maskType = EasyLoadingMaskType.custom
      ..indicatorColor = Colors.white
      ..textColor = Colors.white
      ..maskColor = Colors.black.withOpacity(0.2)
      ..dismissOnTap = false;
    return EasyLoading.init(builder: builder);
  }

  static HZLoadingDialog get instance => _instance;

  static bool get isShow => EasyLoading.instance.w != null;

  /// show loading with [status] [indicator] [maskType]
  static Future<void> showLoading({
    String msg,
    Widget indicator = const CircularProgressIndicator(strokeWidth: 2,color: Colors.white,),
    EasyLoadingMaskType maskType = EasyLoadingMaskType.clear,
    bool dismissOnTap = false,
  }) {
    return EasyLoading.show(
      status: msg,
      indicator: indicator,
      maskType: maskType,
      dismissOnTap: dismissOnTap,
    );
  }
  static Future<void> showSuccess(
      String msg, {
        Duration duration,
        EasyLoadingMaskType maskType = EasyLoadingMaskType.clear,
        bool dismissOnTap = false,
      }) {
    return EasyLoading.showSuccess(
      msg,
      duration: duration ?? EasyLoading.instance.displayDuration,
      maskType: maskType,
      dismissOnTap: dismissOnTap,
    );
  }

  /// showError [status] [duration] [maskType]
  static Future<void> showError(
      String msg, {
        Duration duration,
        EasyLoadingMaskType maskType = EasyLoadingMaskType.clear,
        bool dismissOnTap = false,
      }) {

    return EasyLoading.showError(
      msg,
      duration: duration ?? EasyLoading.instance.displayDuration,
      maskType: maskType,
      dismissOnTap: dismissOnTap,
    );
  }
  static Future<void> showToast(
    String msg, {
    Duration duration = const Duration(milliseconds: 500),
    EasyLoadingToastPosition toastPosition,
    EasyLoadingMaskType maskType = EasyLoadingMaskType.clear,
    bool dismissOnTap = false,
  }) {
    return EasyLoading.showToast(msg,
        duration: duration,
        toastPosition: toastPosition = EasyLoadingToastPosition.center,
        maskType: maskType,
        dismissOnTap: dismissOnTap);
  }
  /// dismiss loading
  static Future<void> dismiss({
    bool animation = true,
  }) {
    return EasyLoading.dismiss(animation: animation);
  }
}
